#include "StdAfx.h"
#include "handler_test.h"
using fly::jld::test::handler_test;

TEST_F(handler_test, reg_empty_name)
{
	User user;
	user.set_action(CREATE);
	user.set_password("123456");
	encode_header(user, USER);

	EXPECT_TRUE(handle_header());

	encode_body(user);

	EXPECT_FALSE(handle_body());
}

TEST_F(handler_test, reg_empty_pwd)
{
	User user;
	user.set_action(CREATE);
	user.set_account("jackong");
	encode_header(user, USER);

	EXPECT_TRUE(handle_header());

	encode_body(user);

	EXPECT_FALSE(handle_body());
}

TEST_F(handler_test, reg_err_format_name)
{
	User user;
	user.set_action(CREATE);
	user.set_account("^&%$~");
	user.set_password("123456");
	encode_header(user, USER);

	EXPECT_TRUE(handle_header());

	encode_body(user);

	EXPECT_FALSE(handle_body());
}

TEST_F(handler_test, reg_exist)
{
	//pre register the account following
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
	DBM->query("INSERT INTO t_user(account, password) VALUES('jackong', '789456')");

	User user;
	user.set_action(CREATE);
	user.set_account("jackong");
	user.set_password("123456");
	encode_header(user, USER);

	EXPECT_TRUE(handle_header());

	encode_body(user);

	EXPECT_TRUE(handle_body());

	decode_header();
	EXPECT_EQ(ACK, header_.pid());

	Ack ack;
	decode_body(ack);
	EXPECT_EQ(USER, ack.what());
	EXPECT_EQ(CREATE, ack.action());
	EXPECT_FALSE(ack.success());
	EXPECT_EQ("This account has been occupied", ack.msg());

	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
}

TEST_F(handler_test, reg_ok)
{
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");

	User user;
	user.set_action(CREATE);
	user.set_account("jackong");
	user.set_password("123456");
	encode_header(user, USER);

	EXPECT_TRUE(handle_header());

	encode_body(user);

	EXPECT_TRUE(handle_body());

//
	decode_header();
	ASSERT_EQ(ACK, header_.pid());

	Ack ack;
	decode_body(ack);
	EXPECT_EQ(USER, ack.what());
	EXPECT_EQ(CREATE, ack.action());
	EXPECT_TRUE(ack.success());

	decode_header();
	ASSERT_EQ(RTT, header_.pid());

	RoundTripTime rtt;
	decode_body(rtt);
	EXPECT_TRUE(rtt.is_proof());
//

	EXPECT_EQ(user.account(), conn_info_.account());

	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
}

TEST_F(handler_test, reg_ok_with_attrs)
{
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
	
	User user;
	user.set_action(CREATE);
	user.set_account("jackong");
	user.set_password("123456");
	Pair* attr = user.add_attributes();
	attr->set_name("name");
	attr->set_value("chain");
	encode_header(user, USER);

	EXPECT_TRUE(handle_header());

	encode_body(user);

	EXPECT_TRUE(handle_body());
//
	decode_header();
	ASSERT_EQ(ACK, header_.pid());

	Ack ack;
	decode_body(ack);
	EXPECT_EQ(USER, ack.what());
	EXPECT_EQ(CREATE, ack.action());
	EXPECT_TRUE(ack.success());

	decode_header();
	ASSERT_EQ(RTT, header_.pid());

	RoundTripTime rtt;
	decode_body(rtt);
	EXPECT_TRUE(rtt.is_proof());
//

	EXPECT_EQ(user.account(), conn_info_.account());

	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
}


TEST_F(handler_test, login_err_pwd)
{
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
	DBM->query("INSERT INTO t_user(account, password) VALUES('jackong', '789456')");

	User user;
	user.set_action(LOGIN);
	user.set_account("jackong");
	user.set_password("123456");
	encode_header(user, USER);

	EXPECT_TRUE(handle_header());

	encode_body(user);

	EXPECT_TRUE(handle_body());

	decode_header();
	EXPECT_EQ(ACK, header_.pid());

	Ack ack;
	decode_body(ack);
	EXPECT_EQ(USER, ack.what());
	EXPECT_EQ(LOGIN, ack.action());
	EXPECT_FALSE(ack.success());
	EXPECT_EQ("Account or password mistake", ack.msg());

	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");

}
TEST_F(handler_test, login_not_exist_account)
{
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");

	User user;
	user.set_action(LOGIN);
	user.set_account("jackong");
	user.set_password("123456");
	encode_header(user, USER);

	EXPECT_TRUE(handle_header());

	encode_body(user);

	EXPECT_TRUE(handle_body());

	decode_header();
	EXPECT_EQ(ACK, header_.pid());

	Ack ack;
	decode_body(ack);
	EXPECT_EQ(USER, ack.what());
	EXPECT_EQ(LOGIN, ack.action());
	EXPECT_FALSE(ack.success());
	EXPECT_EQ("Account or password mistake", ack.msg());
}

TEST_F(handler_test, login_ok)
{
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
	DBM->query("INSERT INTO t_user(account, password) VALUES('jackong', '123456')");

	
	User user;
	user.set_action(LOGIN);
	user.set_account("jackong");
	user.set_password("123456");
	encode_header(user, USER);

	EXPECT_TRUE(handle_header());

	encode_body(user);

	const __int64& start_ = timestamp->cur_micr_sec();
	EXPECT_TRUE(handle_body());
	const __int64& end_ = timestamp->cur_micr_sec();
	LOG_DEBUG("used time[mirc-s]:", integer(end_ - start_));

	EXPECT_EQ(user.account(), conn_info_.account());

	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");


//
	decode_header();
	ASSERT_EQ(ACK, header_.pid());

	Ack ack;
	decode_body(ack);
	EXPECT_EQ(USER, ack.what());
	EXPECT_EQ(LOGIN, ack.action());
	EXPECT_TRUE(ack.success());

	decode_header();
	ASSERT_EQ(RTT, header_.pid());

	RoundTripTime rtt;
	decode_body(rtt);
	EXPECT_TRUE(rtt.is_proof());
//
}